package net.entelijan.cobean.multisel;

abstract class AbstractListItemWrapper<T> implements IListItemWrapper<T> {

	private T item;

	public AbstractListItemWrapper(T item) {
		this.item = item;
	}

	public T getWrappedObject() {
		return this.item;
	}

	@SuppressWarnings("unchecked")
	public int compareTo(Object obj) {
		IListItemWrapper<T> otherItem = (IListItemWrapper<T>) obj;
		return compare(this.item, otherItem.getWrappedObject());
	}

	protected abstract int compare(T litem, T ritem);

	@Override
	public String toString() {
		return toDisplayString(this.item);
	}

	protected abstract String toDisplayString(T item);

	@Override
	public int hashCode() {
		return this.item.hashCode();
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		} else if (obj instanceof IListItemWrapper){
			IListItemWrapper<T> otherItem = (IListItemWrapper<T>) obj;
			return item.equals(otherItem.getWrappedObject());
		} else {
			return false;
		}
	}
}
